package com.nananren.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nananren.anno.OperateLogLog;
import com.nananren.common.Result;
import com.nananren.domain.Employee;
import com.nananren.service.EmployeeService;
import com.nananren.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/employee")
@Slf4j
//@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    // 新增员工
    @PostMapping
    @OperateLogLog
    public Result save(@RequestBody Employee employee) {
        // md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);

        // 新增
        employeeService.save(employee);
        return Result.success("新增成功");
    }

    // 根据id查询员工
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        Employee emp = employeeService.getById(id);
        return emp != null ? Result.success(emp) : Result.error("该用户不存在!!");
    }

    // 根据id修改员工信息
    @PutMapping
    @OperateLogLog
    public Result updateById(@RequestBody Employee employee) {
        // md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
        // 更新修改时间
        employee.setUpdateTime(LocalDateTime.now());
        // 修改
        employeeService.updateById(employee);
        return Result.success("修改成功");
    }

    // 删除员工
    @DeleteMapping("/{id}")
    @OperateLogLog
    public Result deleteById(@PathVariable Long id) {
        boolean flag = employeeService.removeById(id);
        return flag ? Result.success("删除成功") : Result.error("删除的信息不存在");
    }

    // 员工登录
    @PostMapping("/login")
    @OperateLogLog
//    @CrossOrigin
    public Result login(@RequestBody Employee employee, HttpServletRequest httpServletRequest) {
        log.info("token为：{}", httpServletRequest.getHeader("token"));
        log.info("员工登录，信息为：{}", employee.toString());
        // 处理密码
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 根据username和password查询员工
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        employeeLambdaQueryWrapper.eq(Employee::getPassword, password);
        // 查询员工
        Employee employeeInDB = employeeService.getOne(employeeLambdaQueryWrapper);
        if (employeeInDB == null) {
            return Result.error("当前员工不存在");
        }

        // 员工存在，将信息存入token
        Map<String, Object> claims = new HashMap<>();
        // 把id和username存入token
        claims.put("id", employeeInDB.getId());
        claims.put("username", employeeInDB.getUsername());
        // 调用工具类获取token
        String jwt = JwtUtils.getJwt(claims);
        // 将token返回
        return Result.success("登陆成功", jwt);
    }


}
